package com.goethe.translator;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class DieDerDas extends Activity {

	public final static int SPANISH_WORD = 0;
	public final static int SPANISH_ARTICLE = 1;
	public final static int SPANISH_PLURAL = 2;
	public final static int GERMAN_WORD = 3;
	public final static int GERMAN_ARTICLE = 4;
	public final static int GERMAN_PLURAL = 5;
	public final static int WORD_TYPE = 6;
	public final static int WORD_GROUP = 7;
	public final static int WORD_LEVEL = 8;
	
	private ArrayList<String> response;
	private int counterCorrect = 0;
	private int counterIncorrect = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_die_der_das);
        
        getRandomWord();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_die_der_das, menu);
        return true;
    }
    
    private void getRandomWord(){
    	// We make the http request and wait for the results
        DirDerDasHttpRequest httpRequest = new DirDerDasHttpRequest();
        httpRequest.execute();
        response = new ArrayList<String>();
        
		try {
			response = httpRequest.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		// We set the buttons visibility
		findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.VISIBLE);
		findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.GONE);
		
        if(response.isEmpty()){
	        // We set visibility for the views
        	findViewById(R.id.connectionFailure).setVisibility(android.view.View.VISIBLE);
	        findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
	        findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
	        findViewById(R.id.random_word).setVisibility(android.view.View.GONE);
	        findViewById(R.id.random_word_translation).setVisibility(android.view.View.GONE);
        }else{
	        // We set visibility for the views
        	findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
	        findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
	        findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
	        findViewById(R.id.random_word).setVisibility(android.view.View.VISIBLE);
	        findViewById(R.id.random_word_translation).setVisibility(android.view.View.VISIBLE);
	        
	        // We load the random word
	        TextView text = (TextView) findViewById(R.id.random_word);
	        text.setText(response.get(GERMAN_WORD));
	        
	        // We load the random word translation
	        text = (TextView) findViewById(R.id.random_word_translation);
	        text.setText("(" + response.get(SPANISH_WORD) + ")");
        }
    }
    
    public void selectNext(View view){
    	getRandomWord();
    }
    
    public void selectFinish(View view){
        // We set visibility for the views
    	findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
        findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
        findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
        findViewById(R.id.random_word).setVisibility(android.view.View.GONE);
        findViewById(R.id.random_word_translation).setVisibility(android.view.View.GONE);
        findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.GONE);
        findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.GONE);
        findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
        findViewById(R.id.total_correct_message).setVisibility(android.view.View.VISIBLE);
        findViewById(R.id.total_incorrect_message).setVisibility(android.view.View.VISIBLE);

        
        // We concatenate the results to the proper views
        TextView text = (TextView) findViewById(R.id.total_correct_message);
        text.setText(getString(R.string.total_correct) + " " + counterCorrect);
        text = (TextView) findViewById(R.id.total_incorrect_message);
        text.setText(getString(R.string.total_incorrect) + " " + counterIncorrect);
    }
    
    public void selectDie(View view){
    	selectOption("die");
    }
    
    public void selectDer(View view){
    	selectOption("der");
    }
    
    public void selectDas(View view){
    	selectOption("das");
    }
    
    private void selectOption(String option){
    	if(option.equalsIgnoreCase(response.get(GERMAN_ARTICLE))){
	        // We set visibility for the views
	        findViewById(R.id.correct_message).setVisibility(android.view.View.VISIBLE);
	        findViewById(R.id.incorrect_message).setVisibility(android.view.View.GONE);
	        
	        // We increment the correct counter
	        counterCorrect+=1;
    	}else{
	        // We set visibility for the views
	        findViewById(R.id.correct_message).setVisibility(android.view.View.GONE);
	        findViewById(R.id.incorrect_message).setVisibility(android.view.View.VISIBLE);
	        
	        // We increment the correct counter
	        counterIncorrect+=1;
    	}
    	
        // We set the correction message
        TextView text = (TextView) findViewById(R.id.random_word);
        text.setText(response.get(GERMAN_ARTICLE) + " " + response.get(GERMAN_WORD));
    	
		// We set the buttons visibility
		findViewById(R.id.buttonsDieDerDas).setVisibility(android.view.View.GONE);
		findViewById(R.id.buttonsNextFinish).setVisibility(android.view.View.VISIBLE);
    }
}
